//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    // 上拉加载
    allGoodsData: [],
    pageIndex: 1,
    state: "",
    // 
    userType:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userType: wx.getStorageSync('userType'),
    })
    this.GetGoodsListByType();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageIndex++;
    this.GetGoodsListByType();
  },

  // 
  GetGoodsListByType: function () {
    wx.showLoading({
      title: '加载中',
      icon: 'success',
      duration: 2000
    })
    var that = this;
    var allGoodsData = this.data.allGoodsData;
    var pageIndex = this.data.pageIndex;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetGoodsListByType&PageIndex=" + pageIndex + "&PageSize=10&Type=4"),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          allGoodsData.push(json[i])
        }
        that.setData({
          allGoodsData: allGoodsData,
        })
        console.log(allGoodsData);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();

      }
    })
  },
  // 个人抢红包
  ticketTap:function(e){
    var that = this;
    var uid = this.data.uid;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=ReplySystemMessage&SJID=" + e.currentTarget.dataset.sjid + "&MessageId=" + e.currentTarget.dataset.hongbaoid + "&Content=抢"),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultData == 'success') {
          wx.showModal({
            content: '恭喜！您获得了一个新红包！',
            confirmText: "去看看",
            cancelText: "知道了",
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确认')
                wx.navigateTo({
                  url: '/packageA/ticketList/ticketList',
                })

              } else {

              }
            },
          });
        } else {
          wx.showToast({
            title: "很抱歉！暂未抢到红包。",
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 
})